package com.jilian.mydemo.modul.order.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.contrarywind.view.WheelView;
import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.OrderDto;
import com.jilian.mydemo.dialog.nicedialog.BaseNiceDialog;
import com.jilian.mydemo.dialog.nicedialog.NiceDialog;
import com.jilian.mydemo.dialog.nicedialog.ViewConvertListener;
import com.jilian.mydemo.dialog.nicedialog.ViewHolder;
import com.jilian.mydemo.modul.order.adapter.WheelAdapter;
import com.jilian.mydemo.modul.order.viewmodel.OrderViewModel;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.ToastUitl;

import java.util.ArrayList;
import java.util.List;

public class SingleBackActivity  extends BaseActivity {
    private List<String> datas;
    private RelativeLayout rlBack;
    private TextView tvOk;
    private TextView tvSelect;
    private TextView tvNumber;
    private TextView tvType;
    private TextView tvName;
    private TextView tvDate;
    private TextView tvAdress;
    private EditText etContent;



    private OrderViewModel orderViewModel;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }



    @Override
    protected void createViewModel() {
        orderViewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_singleback;
    }

    @Override
    public void initView() {
        setNormalTitle("退单", v -> finish());
        rlBack = (RelativeLayout) findViewById(R.id.rl_back);
        tvOk = (TextView) findViewById(R.id.tv_ok);
        tvSelect = (TextView) findViewById(R.id.tv_select);
        tvNumber = (TextView) findViewById(R.id.tv_number);
        tvType = (TextView) findViewById(R.id.tv_type);
        tvName = (TextView) findViewById(R.id.tv_name);
        tvDate = (TextView) findViewById(R.id.tv_date);
        tvAdress = (TextView) findViewById(R.id.tv_adress);
        etContent = (EditText) findViewById(R.id.et_content);
        datas = new ArrayList<>();
        datas.add("客户临时不在家");
        datas.add("客户临时不在家");
        datas.add("客户临时不在家");
        datas.add("客户临时不在家");
        datas.add("客户临时不在家");

    }
    private  OrderDto orderDto;
    @Override
    public void initData() {
        orderDto = (OrderDto) getIntent().getSerializableExtra("order");
        if(EmptyUtils.isNotEmpty(orderDto)){
            tvNumber.setText(orderDto.getOrderNo());
            tvType.setText(orderDto.getServiceType());
            tvName.setText(orderDto.getCustomerName());
            tvDate.setText(orderDto.getBookTime());
            tvAdress.setText(orderDto.getServiceAddress());
        }
    }
    /**
     * 退单对话框
     */
    private void showRefundReasonDialog() {

        NiceDialog.init()
                .setLayoutId(R.layout.dialog_select_courier)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                        dialog.setOutCancel(false);
                        WheelView wheelview = (WheelView) holder.getView(R.id.wheelview);
                        wheelview.setCyclic(false);
                        TextView tvCancel = (TextView) holder.getView(R.id.tv_cancel);
                        TextView tvFinish = (TextView) holder.getView(R.id.tv_finish);
                        wheelview.setAdapter(new WheelAdapter(datas));

                        tvFinish.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                tvSelect.setText(datas.get(wheelview.getCurrentItem()));
                                tvOk.setEnabled(true);
                                tvOk.setBackgroundResource(R.drawable.shape_btn_login_normal);
                                dialog.dismiss();
                            }
                        });
                        tvCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dialog.dismiss();
                            }
                        });
                        wheelview.setCurrentItem(0);
                    }
                })
                .setShowBottom(true)
                .show(getSupportFragmentManager());
    }
    @Override
    public void initListener() {
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showRefundReasonDialog();
            }
        });
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                withdrawOrder();
            }
        });
    }

    /**
     * 退单
     */
    private void withdrawOrder() {
        showLoadingDialog();
        orderViewModel.withdrawOrder(orderDto.getId(),etContent.getText().toString(),tvSelect.getText().toString());
        orderViewModel.getWithdrawliveData().observe(this, new Observer<BaseDto>() {
            @Override
            public void onChanged(@Nullable BaseDto dto) {
                hideLoadingDialog();
                if(dto.isSuccess()){
                    ToastUitl.showImageToastSuccess(dto.getMsg());
                    finish();
                }
                else{
                    ToastUitl.showImageToastTips(dto.getMsg());
                }
            }
        });
    }
}
